package cn.java79.springboot.zsb.ctrl;

import cn.java79.springboot.zsb.pojo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate template;

    //Class,name,code,uid
    @PostMapping("login")
    public AjaxResult login(@RequestBody Map<String,String> params) {
        String name=params.get("name");
        String pass=params.get("pass");
        String vcode=params.get("vcode");
        String uid=params.get("uid");
        System.out.println(name+","+pass+","+vcode+","+uid);
        String  code =(String) template.opsForValue().get("vcode::"+uid);
        System.out.println(">>>>>>>>>>>>>>>>>>>>"+code);
        if(code==null){
            return AjaxResult.error("验证码没有找到或者已经失效");
        }
        if(!vcode.equals(code)){
            return AjaxResult.error("验证码输入错误");
        }
        //db验证没有实现
        if("admin".equals(name))
            return AjaxResult.success();

        return AjaxResult.error("用户名或者密码输入错误");
    }
//    public AjaxResult login(String u,String p,String code,@CookieValue("piccode") String realCode){
//        return AjaxResult.success();
//    }
}
